package pers.qianyu.month_202012.date_20201210;

import org.junit.*;

import java.util.*;

/**
 * [巧妙]
 * 78. 子集
 * https://leetcode-cn.com/problems/subsets/
 *
 * @author mizzle rain
 * @date 2020-12-10 17:07
 */
public class Subsets4 {
    /**
     * 二进制法
     */
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < (1 << nums.length); i++) {
            ArrayList<Integer> list = new ArrayList<>();
            for (int j = 0; j < nums.length; j++) {
                if (((i >> j) & 1) == 1) {
                    list.add(nums[j]);
                }
            }
            res.add(list);
        }
        return res;
    }

    @Test
    public void test1() {
        int[] arr = {1, 2, 3};
        List<List<Integer>> result = new Subsets4().subsets(arr);
        result.forEach(System.out::println);
    }
}
